package panda.client.ui.widgets;

import panda.client.JSNI;
import panda.client.Cardpanda;
import panda.client.engine.Observable;
import panda.client.engine.Observer;
import panda.client.ui.observable.ObservableLifeTotal;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

public class LifeCounter extends Composite implements Observer {
	private HorizontalPanel shell = new HorizontalPanel();
	private HTML decLife = new HTML("&ndash;");
	private HTML incLife = new HTML("+");
	private Label lifeTotal = new Label("");
	private String owner;

	public LifeCounter(final ObservableLifeTotal observableLifeTotal) {
		observableLifeTotal.addObserver(this);
		this.owner = observableLifeTotal.getOwnerName();

		JSNI.disableSelection(decLife.getElement());
		JSNI.disableSelection(incLife.getElement());

		DOM.setStyleAttribute(decLife.getElement(), "padding", "3px");
		DOM.setStyleAttribute(decLife.getElement(), "borderRight", "1px solid rgb(180, 180, 180)");
		DOM.setStyleAttribute(incLife.getElement(), "padding", "3px");
		DOM.setStyleAttribute(incLife.getElement(), "borderLeft", "1px solid rgb(180, 180, 180)");

		DOM.setStyleAttribute(lifeTotal.getElement(), "textAlign", "center");
		DOM.setStyleAttribute(lifeTotal.getElement(), "padding", "3px");

		DOM.setStyleAttribute(shell.getElement(), "border", "1px solid rgb(180, 180, 180)");

		decLife.addMouseListener(new MouseListener() {
			public void onMouseDown(Widget sender, int x, int y) {
				setMouseDownStyles(decLife);
			}

			public void onMouseEnter(Widget sender) {
				setMouseEnterStyles(decLife);
			}

			public void onMouseLeave(Widget sender) {
				setMouseLeaveStyles(decLife);
			}

			public void onMouseMove(Widget sender, int x, int y) {}

			public void onMouseUp(Widget sender, int x, int y) {
				observableLifeTotal.setLifeTotal(observableLifeTotal.getLifeTotal() - 1);
				setMouseUpStyles(decLife);
				
				String name = Cardpanda.clientModel.getSessionID().getName();
				String logForMe = "You decrement " + owner + "'s life total.  New life total: " + observableLifeTotal.getLifeTotal();
				String logForOthers = name + " decrements " + owner + "'s life total.  New life total: " + observableLifeTotal.getLifeTotal();
				NoteTip.get().log(logForMe, logForOthers);
				
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());	
			}
		});

		incLife.addMouseListener(new MouseListener() {
			public void onMouseDown(Widget sender, int x, int y) {
				setMouseDownStyles(incLife);
			}

			public void onMouseEnter(Widget sender) {
				setMouseEnterStyles(incLife);
			}

			public void onMouseLeave(Widget sender) {
				setMouseLeaveStyles(incLife);
			}

			public void onMouseMove(Widget sender, int x, int y) {}

			public void onMouseUp(Widget sender, int x, int y) {
				int newTotal = observableLifeTotal.getLifeTotal() + 1;				
				observableLifeTotal.setLifeTotal(newTotal);
				setMouseUpStyles(incLife);
				
				String name = Cardpanda.clientModel.getSessionID().getName();
				String logForMe = "You increment " + owner + "'s life total.  New life total: " + observableLifeTotal.getLifeTotal();
				String logForOthers = name + " increments " + owner + "'s life total.  New life total: " + observableLifeTotal.getLifeTotal();
				NoteTip.get().log(logForMe, logForOthers);

				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());			
			}
		});

		shell.add(decLife);
		shell.add(lifeTotal);
		shell.add(incLife);
		initWidget(shell);
		update(observableLifeTotal, null);
	}

	private void setMouseUpStyles(Label incDec) {
		DOM.setStyleAttribute(incDec.getElement(), "backgroundColor", "rgb(230, 230, 230)");
	}

	private void setMouseDownStyles(Label incDec) {
		DOM.setStyleAttribute(incDec.getElement(), "backgroundColor", "rgb(240, 240, 240)");
	}

	private void setMouseEnterStyles(Label incDec) {
		DOM.setStyleAttribute(incDec.getElement(), "backgroundColor", "rgb(230, 230, 230)");
		DOM.setStyleAttribute(incDec.getElement(), "cursor", "pointer");
	}

	private void setMouseLeaveStyles(Label incDec) {
		DOM.setStyleAttribute(incDec.getElement(), "backgroundColor", "white");
		DOM.setStyleAttribute(incDec.getElement(), "cursor", "default");
	}

	public void update(Observable o, Object arg) {
		ObservableLifeTotal life = (ObservableLifeTotal) o;
		lifeTotal.setText(life.getLifeTotal() + "");
	}
	
	public String getOwner() {
		return owner;
	}
}